﻿/***************************************************************************
//* Foxit Software Inc.
//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
//* and trade secrets of Foxit Software Inc.                                                      *
***************************************************************************/
/**
* Markup Annot 
*/
define(function (require, exports, module) {
    var CPDF_Annot = require('./Annot.js');
    var InterfaceImpl = require('../Interface');


    var AnnotDataFmt = FXREADER.PDFData.AnnotDataJsonFormat;
    FXREADER.PDFData.CPDF_MarkupAnnot = {};
    //Popup Annot
    var CPDF_PopupAnnot = function (jsonData) {
        CPDF_PopupAnnot.superclass.constructor.call(this, jsonData);
    };
    InterfaceImpl.extend(CPDF_PopupAnnot, CPDF_Annot);

    CPDF_PopupAnnot.prototype.GetOpenState = function () {
        return this.m_AnnotData[AnnotDataFmt.OpenState];
    };

    CPDF_PopupAnnot.prototype.SetOpenState = function (state) {
        this.m_AnnotData[AnnotDataFmt.OpenState] = state;
    };

    CPDF_PopupAnnot.prototype.GetModifyDate = function () {
        return this.m_AnnotData[AnnotDataFmt.ModifyDate];
    };
    FXREADER.PDFData.CPDF_PopupAnnot = CPDF_PopupAnnot;

    //Markup Annot State 
    var CPDF_AnnotState = function (jsonData) {
        this.m_StateData = jsonData;
    };

    CPDF_AnnotState.prototype.GetState = function () {
        return this.m_StateData[AnnotDataFmt.State];
    };

    CPDF_AnnotState.prototype.GetStateMode = function () {
        return this.m_StateData[AnnotDataFmt.StateModel];
    };
    FXREADER.PDFData.CPDF_AnnotState = CPDF_AnnotState;

    //Markup Annot
    var CPDF_MarkupAnnot = function (jsonData) {
        CPDF_MarkupAnnot.superclass.constructor.call(this, jsonData);
        this.Popup = null;
        this.ReplyChain = null;
        this.StateList = null;
        this.m_GroupAnnots = []; //只是引用Page里面的Annot
        this.m_HeadAnnot = null; //所在的group中的主annot
        /* this.bRopeShowed = 0;*/
    };
    InterfaceImpl.extend(CPDF_MarkupAnnot, CPDF_Annot);

    CPDF_MarkupAnnot.prototype.GetOpcity = function () {
        return this.m_AnnotData[AnnotDataFmt.Opcity];
    };

    CPDF_MarkupAnnot.prototype.GetCreationDate = function () {
        return this.m_AnnotData[AnnotDataFmt.CreationDate];
    };

    CPDF_MarkupAnnot.prototype.GetModifyDate = function () {
        return this.m_AnnotData[AnnotDataFmt.ModifyDate];
    };

    CPDF_MarkupAnnot.prototype.GetSubject = function () {
        return this.m_AnnotData[AnnotDataFmt.Subject];
    };

    CPDF_MarkupAnnot.prototype.GetTitle = function () {
        return this.m_AnnotData[AnnotDataFmt.Title];
    };

    CPDF_MarkupAnnot.prototype.GetPopup = function () {
        if (this.Popup == null) {
            if (this.m_AnnotData[AnnotDataFmt.Popup] == null)
                return null;
            this.Popup = new CPDF_PopupAnnot(this.m_AnnotData[AnnotDataFmt.Popup]);
        }
        return this.Popup;
    };

    CPDF_MarkupAnnot.prototype.GetSubType = function () {
        return this.m_AnnotData[AnnotDataFmt.SubType];
    };

    CPDF_MarkupAnnot.prototype.GetTextAnnotName = function () {
        return this.m_AnnotData[AnnotDataFmt.TextAnnotName];
    };


    //     CPDF_MarkupAnnot.prototype.GetRopeState = function () {
    //         return this.bRopeShowed;
    //     }
    // 
    //     CPDF_MarkupAnnot.prototype.SetRopeState = function (state) {
    //         this.bRopeShowed = state;
    //     }

    CPDF_MarkupAnnot.prototype.InitReplyChain = function () {
        if (this.ReplyChain == null) {
            this.ReplyChain = new Array();
            if (this.m_AnnotData[AnnotDataFmt.ReplyChain] == null) {
                return;
            }
            var iReplyCnt = this.m_AnnotData[AnnotDataFmt.ReplyChain].length;
            for (var i = 0; i < iReplyCnt; i++) {
                this.ReplyChain[i] = new CPDF_MarkupAnnot(this.m_AnnotData[AnnotDataFmt.ReplyChain][i]);
                this.ReplyChain[i].InitReplyChain();
            }
        }
    };

    CPDF_MarkupAnnot.prototype.GetReplyChain = function () {
        this.InitReplyChain();
        return this.ReplyChain;
    };

    CPDF_MarkupAnnot.prototype.InitStateList = function () {
        if (this.StateList == null) {
            this.StateList = new Array();
            if (this.m_AnnotData[AnnotDataFmt.StateList] == null) {
                return;
            }
            var iStateCnt = this.m_AnnotData[AnnotDataFmt.StateList].length;
            for (var i = 0; i < iStateCnt; i++) {
                this.StateList[i] = new CPDF_AnnotState(this.m_AnnotData[AnnotDataFmt.StateList][i]);
            }
        }
    };

    CPDF_MarkupAnnot.prototype.AddAnnot2Group = function (pAnnot) {
        if (pAnnot == null)
            return false;
        this.m_GroupAnnots.push(pAnnot);
        return true;
    };

    CPDF_MarkupAnnot.prototype.CountGroupAnnots = function () {
        return this.m_GroupAnnots.length;
    };

    CPDF_MarkupAnnot.prototype.GetAnnotFromGroup = function (iAnnotIndex) {
        return this.m_GroupAnnots[iAnnotIndex];
    };

    CPDF_MarkupAnnot.prototype.GetGroupAnnotList = function () {
        return this.m_GroupAnnots;
    };

    CPDF_MarkupAnnot.prototype.GetHeadAnnot = function () {
        return this.m_HeadAnnot;
    };

    CPDF_MarkupAnnot.prototype.GetHeadAncestorAnnot = function () {
        var pHeadAnnot = this.m_HeadAnnot;
        while (pHeadAnnot) {
            if (pHeadAnnot.GetType() == "Markup") {
                if(pHeadAnnot.GetHeadAnnot())
                    pHeadAnnot = pHeadAnnot.GetHeadAnnot(); 
                else
                    return pHeadAnnot;
            }
            else
                return pHeadAnnot;
        }
        return pHeadAnnot;
    };


    CPDF_MarkupAnnot.prototype.SetHeadAnnot = function (pHeadAnnot) {
        this.m_HeadAnnot = pHeadAnnot;
    };

    CPDF_MarkupAnnot.prototype.GetStateList = function () {
        this.InitStateList();
        return this.StateList;
    };

    FXREADER.PDFData.CPDF_MarkupAnnot = CPDF_MarkupAnnot;

    return FXREADER.PDFData.CPDF_MarkupAnnot;
});